import * as rq from 'request-promise';
import env from './../config/env';
import loginUtils from './../core/LoginUtils';
import * as moment from 'moment';
import logger from '../lib/Log4js';
import { returnInfoModel } from '../model/returnInfoModel';
import { returnListModel } from '../model/returnListModel';

export class ThhService {

    /**
     * @description 测试接口  查询数据用
     * @author 阳朔
     */
    async testGetReturnInfo () {

        let findList :any = await rq.post(`${env.b2bUrl}/portal/control/wsif/getReturnInfo`,
        {
            body: {
                'facilityId': '11100',           //仓库编码
                returnId : 'C120170',
                // statusId : 'RETURN_PENDING',
                'login.username': env.loginUsername,  //用户名
                'login.password': env.loginPwd        //密码
            },
            json: true
        })

        return findList ;
    }


    /**
     * @description 退货单查询     获取根据仓库关联的所有可查询的退货单列表菜单
     * @author 阳朔
     * @param {any} res 
     * @param {any} authorization 
     * @returns 
     */
    async getReturnInfoList ( res ,authorization ){

        let checkNum: number = await loginUtils.checkUser(authorization);
        if (checkNum == 101) {
            return { code: checkNum };
        }

        let facilityId :string = res.storcode;
        let returnTypeId :string = res.returnTypeId ;
        let inventoryStatusId :string = res.inventoryStatusId ;

        let findList :any = await rq.post(`${env.b2bUrl}/portal/control/wsif/getReturnInfo`,
        {
            body: {
                'facilityId': facilityId,           //仓库编码
                'returnTypeId': returnTypeId,         //退货单类型
                inventoryStatusId : inventoryStatusId , //货物状态  待确认 RET_INV_SUBMITTED   待取回 RET_INV_PENDING
                'login.username': env.loginUsername,  //用户名
                'login.password': env.loginPwd        //密码
            },
            json: true
        })

        if(findList.responseMessage != 'success'){
            return { code : 102 , msg : findList.errorMessage }
        }

        let returnInfoList :any[] = [];
        let returnMenuList :any[] = [];

        for (let i = 0; i < findList.returnList.length; i++) {//遍历所有数据
            let element = findList.returnList[i];
            for (let j = 0; j < element.productInfo.length; j++) {//获取所有关联 仓库 的商品列表
                let product = element.productInfo[j];
                returnInfoList.push({
                    returnId :element.returnId ,      //退货单号   根据它查
                    facilityId:facilityId,      //仓库编码   根据它查
                    returnItemSeqId:product.returnItemSeqId, //行项目号
                    productCode:product.productCode,     //商品编码
                    productName:product.productName,     //商品名称
                    unit:product.unit,            //单位
                    quantity:product.quantity,        //订单数量
                    returnQuantity:product.returnQuantity,  //退货数量
                    changeQuantity:product.changeQuantity  //换货数量
                })
            }
            returnMenuList.push({
                returnId :element.returnId ,      //退货单号   根据它查
                facilityId:facilityId,      //仓库编码   根据它查
                orderId:element.baseInfo.orderId,         //订单号
                returnTypeId:element.baseInfo.returnTypeId,    //退货单类型
                statusId:element.baseInfo.statusId,        //退货单状态
                statusName:element.baseInfo.statusName,        //退货单状态
                createdStamp:element.baseInfo.createdStamp    //创建时间
            })
        }

        return { code : 100 , data : returnMenuList };
    }


    /**
     * @description 扫描退货单号 并根据仓库编号 查询退货单
     * @author 阳朔
     * @param {any} res 
     */
    async getReturnInfo (res , authorization){
        
        let checkNum: number = await loginUtils.checkUser(authorization);
        if (checkNum == 101) {
            return { code: checkNum };
        }

        let facilityId :string = res.storcode;
        let returnId :string = res.returnId ;
        let returnTypeId :string = ''//res.returnTypeId ;

        let findList :any = await rq.post(`${env.b2bUrl}/portal/control/wsif/getReturnInfo`,
        {
            body: {
                'facilityId': facilityId,           //仓库编码
                'returnId': returnId,                 //退货单号
                // 'returnTypeId': returnTypeId,         //退货单类型
                'login.username': env.loginUsername,  //用户名
                'login.password': env.loginPwd        //密码
            },
            json: true
        })

        if(findList.responseMessage != 'success'){
            return { code : 102 , msg : findList.errorMessage }
        }

        return { code : 100 , data : findList };

    }


    /**
     * @description 销售退货单  待确认
     *                  带取回 提交
     * @author 阳朔
     * @param {any} res 
     * @param {any} authorization 
     * @returns 
     */
    async submitReturnInfo ( res , authorization){
        let checkNum: number = await loginUtils.checkUser(authorization);
        if (checkNum == 101) {
            return { code: checkNum };
        }

        let user_info = loginUtils.decode(authorization);
        let opeUser: string = user_info.username;
        let returnId = res.returnId ;
        let action = res.action ;
        let reason = res.reason ;
        let productInfo = res.productInfo ;

        let signImg :string = res.signImg ;

        let productList :any[] = [];
        for (let i = 0; i < productInfo.length; i++) {
            let element = productInfo[i];
            productList.push({
                returnItemSeqId : element.returnItemSeqId ,
                returnQuantity : element.returnQuantity ,
                changeQuantity : element.changeQuantity
            })
        }
        let body :any = {} ;
        if(signImg){
            body = {
                'returnId': returnId,                 //退货单号
                'signImg': signImg,                   
                'action' : action,                    //四个动作
                'opeUser' : opeUser ,                 //PDA用户名
                'login.username': env.loginUsername,  //用户名
                'login.password': env.loginPwd ,      //密码
                'productInfo' : productList 
            }
        }else{
            body = {
                'returnId': returnId,                 //退货单号
                'action' : action,                    //四个动作
                'reason' : reason ,                   //仓库接受/拒绝理由
                'opeUser' : opeUser ,                 //PDA用户名
                'login.username': env.loginUsername,  //用户名
                'login.password': env.loginPwd ,      //密码
                'productInfo' : productList 
            }
        }

        let json :any = await rq.post(`${env.b2bUrl}/portal/control/wsif/confirmReturnInfo`,
        {
            body: body ,
            json: true
        })

        if(json.responseMessage != 'success'){
            return { code : 102 , msg : json.errorMessage }
        }

        return { code : 100 }
    }



/* *************************************** */

    async getReturnInfoList2 ( res ,authorization ){
    
        let checkNum: number = await loginUtils.checkUser(authorization);
        if (checkNum == 101) {
            return { code: checkNum };
        }

        let facilityId :string = res.storcode;
        let returnId :string = res.returnId ;

        let findList :any = await rq.post(`${env.b2bUrl}/portal/control/wsif/getReturnInfo`,
        {
            body: {
                'facilityId': facilityId,           //仓库编码
                'returnId': returnId,        
                'login.username': env.loginUsername,  //用户名
                'login.password': env.loginPwd        //密码
            },
            json: true
        })

        if(findList.responseMessage != 'success'){
            return { code : 102 , msg : findList.errorMessage }
        }

        let returnMenuList :any[] = [];

        for (let i = 0; i < findList.returnList.length; i++) {//遍历所有数据
            let element = findList.returnList[i];
            returnMenuList.push({
                returnId :element.returnId ,      //退货单号   根据它查
                facilityId:facilityId,      //仓库编码   根据它查
                orderId:element.baseInfo.orderId,         //订单号
                returnTypeId:element.baseInfo.returnTypeId,    //退货单类型
                statusId:element.baseInfo.statusId,        //退货单状态
                statusName:element.baseInfo.statusName,        //退货单状态
                createdStamp:element.baseInfo.createdStamp    //创建时间
            })
        }

        return { code : 100 , data : returnMenuList };
    }

    async getReturnInfo2 (res , authorization){
        
        let checkNum: number = await loginUtils.checkUser(authorization);
        if (checkNum == 101) {
            return { code: checkNum };
        }

        let facilityId :string = res.storcode;
        let returnId :string = res.returnId ;
        let returnTypeId :string = res.returnTypeId ;

        let findList :any = await rq.post(`${env.b2bUrl}/portal/control/wsif/getReturnInfo`,
        {
            body: {
                'facilityId': facilityId,           //仓库编码
                'returnId': returnId,                 //退货单号
                'returnTypeId': returnTypeId,         //退货单类型
                'login.username': env.loginUsername,  //用户名
                'login.password': env.loginPwd        //密码
            },
            json: true
        })

        if(findList.responseMessage != 'success'){
            return { code : 102 , msg : findList.errorMessage }
        }

        return { code : 100 , data : findList };

    }


    

}


let thhService = new ThhService();
export default thhService;